/**
 *  Date           	Name    		     Change
 *  23/09/2014     	Roy Chan			 Creation
 *
 */
package utilities.stemmer;

import java.util.HashSet;
import java.util.Set;

import files.GetResourcesPath;
import utilities.FileInput;

/**
 * @author Roy Chan
 *
 */
public class StopWordFinder {
	Set<String> stopWordSet;
	
	public StopWordFinder(){
		LoadStopWord();
	}
	
	/**
	 * Read file: estop.txt
	 */
	public void LoadStopWord(){
		//String fileAddress = "C:\\Users\\asl\\Desktop\\Roy\\workspace\\Comp433\\Comp433\\src\\files\\estop.txt";
		String fileAddress = new GetResourcesPath().getPath("estop.txt");
		FileInput fileEstop = new FileInput(fileAddress);
		stopWordSet = new HashSet<String>();
		while(fileEstop.hasNextLine())
		{
			String stopWord = fileEstop.nextLine();
			stopWordSet.add(stopWord);
		}
	}

	/**
	 * return true if it is Stop Word.
	 * @param word
	 * @return isStop
	 */
	public boolean isStopWord(String word){
		boolean isStop = false;
		word = word.toLowerCase();
		
		char[] wordArr = word.toCharArray();
		int wordLength = wordArr.length-1;

		while( (wordLength > 0) && !Character.isLetter(wordArr[wordLength]) && !Character.isDigit(wordArr[wordLength]) )
		{
			word = word.substring(0, wordLength);
			wordLength--;
		}
		
		if(stopWordSet.contains(word))
			isStop = true;
		
		return isStop;
	}
	

}
